import './profile.less';
import Header from "../../../components/Header";
import {Link} from "react-router";
import {connect} from "react-redux";
import NoData from "../../../components/nodata";
import Search from "../../../components/search";
import Panel from "../../../elements/Panel";
import Pagination from "../../../components/Pagination/";
import Config from "../../../libs/config";
import {SUBJECT, PAGE_SUBJECT} from "../../../actions/subject";
import {fetch, fetch_remove, enable_delete_btn, selectAllItem, selectItem} from "../../../actions";
import {md5Url, URL} from '../../../libs/Url';
import {Fetch} from '../../../libs/';
import FormItemRef from '../../../components/FormItemRef';
import FormItemType from '../../../components/FormItemType';

const Feedback = React.createClass({
    getInitialState(){
        return {
            user: {},
            editing: true,
            label: '修改资料',
            rolename:[],
            form: [
                {
                    name: 'name',
                    label: '姓名',
                    value: '',
                    required: false,
                    type: FormItemType.TEXT_SHOW,
                },
                {
                    name: 'gender',
                    label: '姓别',
                    value: '',
                    value_: '',
                    required: false,
                    type: FormItemType.TEXT_SHOW,
                },
                {
                    name: 'duty',
                    label: '职务',
                    value: '职务',
                    required: false,
                    type: FormItemType.TEXT_SHOW,
                },
                {
                    name: 'title',
                    label: '任教科目',
                    value: '',
                    required: false,
                    type: FormItemType.TEXT_SHOW,
                },
                {
                    name: 'phone',
                    label: '手机',
                    value: '',
                    required: false,
                    type: FormItemType.TEXT_SHOW,
                },
                {
                    name: 'email',
                    label: '邮箱',
                    value: '',
                    required: false,
                    type: FormItemType.TEXT_SHOW,
                },
                {
                    name: 'email',
                    label: '任教班级',
                    value: '',
                    required: false,
                    type: FormItemType.TEXT_SHOW,
                },
                {
                    name: 'email',
                    label: '角色',
                    value: '',
                    required: false,
                    type: FormItemType.TEXT_SHOW,
                },
                {
                    name: 'profile',
                    label: '教师简介',
                    value: '',
                    required: false,
                    type: FormItemType.TEXT_SHOW,
                    onchange: function (index, content) {
                        console.log(content)
                        self.setState({
                            form: [...self.form.slice(0, index), Object.assign({}, self.form[index], {value: content})]
                        })
                    }
                },

            ]
        }
    },
    componentDidMount(){
        Fetch(URL.Get_USER)
            .then(res=> {
                console.log(res);
                this.setState({
                    user: res.user,
                    rolename:res.rolename
                })
            })
            .catch(e=> {
                console.log(e)
            })
    },

    render(){
        const self = this;
        const {dispatch, user} = this.props;
        // console.log(user);
        var user_ = this.state.user;
        const data = self.props || {};
        var form = this.state.form;
        form.map((item, index)=> {
            if (index == 1) {
                if (user.teacher)
                    item.value = (user.teacher && user.teacher.gender) ? '男' : '女';
            } else if (index == 2) {
                item.value = user.teacher && user.teacher.duty
            } else if (index == 3) {
                if (user.teacher && user.teacher.subject)
                    item.value = user.teacher.subject.name;
            } else if (index == 6) {
                if (user['class'])
                    item.value = user['class'].map((item)=> {
                        return item['class'].name
                    }).toString();
            } else if (index == 8) {
                if (user['teacher'])
                    item.value = item.value || (user_['teacher'] && user_['teacher'].comment);
            } else if (index == 7) {
                item.value = self.state.rolename.map(item=>{return item['rolename']}).toString();
            } else {
                item.value = user[item.name] || ' ';
            }
            return item;
        });
        return (
            <Panel>
                <Header>个人资料</Header>
                <div className="table-list-head row profile">
                    {
                        form.slice(0, 8).map((item, index)=> {
                            return <div
                                key={index}
                                className="form-group clearfix" style={{marginBottom: '15px'}}>
                                <label
                                    className="col-sm-2 control-label">{item.label}：</label>
                                <div
                                    className="col-sm-10">
                                    <div style={{paddingTop: '7px', minHeight: 25}}>{item.value}</div>
                                </div>
                            </div>
                        })
                    }

                    <FormItemRef
                        form={form.slice(8)}
                        editing={this.state.editing}
                        addM={true}
                        labelAlign="right"
                        label={this.state.label}
                        onsubmit={(data)=> {
                            var c = {};
                            if (this.state.editing) {
                                c.label = '保存';
                                c.editing = false;
                                c.form = [...this.state.form.slice(0, 8), Object.assign({}, this.state.form[8], {type: FormItemType.TEXT_AREA})];
                                this.setState(c);
                            } else {
                                Fetch(URL.USER_UPDATE_PROFILE, {
                                    body: {comment: data[0].value}
                                }).then(res=> {
                                    console.log(res);
                                    console.log(data[0].value);
                                    c.label = '编辑资料';
                                    c.editing = true;
                                    c.form = [...this.state.form.slice(0, 8),
                                        Object.assign({}, this.state.form[8], {
                                            type: FormItemType.TEXT_SHOW,
                                            value: data[0].value
                                        })];
                                    self.setState(c)
                                }).catch(e=> {
                                    console.log(e);
                                })
                            }
                        }}
                    />
                </div>
            </Panel>
        )
    }
});
function select(state) {
    return {
        user: state.user.user,
    }
}
export default connect(select)(Feedback)